<template>
  <el-container>
    <el-aside>

      <div>
        <el-button v-if="isAuth('sys:user:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('sys:user:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </div>

      <el-form class="el-form1" :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">

        <el-form-item>
          <el-input class="mod-input" v-model="dataForm.userName" placeholder="用户名" clearable maxlength="20"></el-input>
        </el-form-item>

        <el-form-item>
          <el-input v-model="deptForm.parentName" v-popover:deptListPopover clearable :readonly="true"
            placeholder="下面选择角色所属部门" class="menu-list__input"></el-input>
          <el-button class="el-button1" size="mini" @click="getDataList()">查询</el-button>
          <el-button class="el-button1" size="mini" @click="resetFields()" type="danger">重置</el-button>
        </el-form-item>

        <el-tree :data="deptList" :props="deptListTreeProps" node-key="id" ref="deptListTree"
          @current-change="deptListTreeCurrentChangeHandle" :default-expand-all="true" :highlight-current="true"
          :expand-on-click-node="false">
        </el-tree>


      </el-form>
    </el-aside>

    <el-container>

      <el-main>
        <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
          style="width: 100%;" :row-class-name="tableRowClassName">
          <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>

          <el-table-column prop="username" header-align="center" align="center" label="用户名"></el-table-column>

          <el-table-column prop="roleStrList" header-align="center" align="center" label="角色" min-width="200">
            <template slot-scope="scope">
              <el-tag v-for="roleStr in scope.row.roleStrList" :key="roleStr" size="small"
                type="success">{{roleStr}}</el-tag>
            </template>
          </el-table-column>

          <el-table-column prop="status" header-align="center" align="center" label="状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 0" size="small" type="danger">禁用</el-tag>
              <el-tag v-else size="small">启用</el-tag>
            </template>
          </el-table-column>

          <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
            <template slot-scope="scope">
              <el-button v-if="isAuth('sys:user:info')" type="text" size="small"
                @click="detailHandle(scope.row.userId)">详情</el-button>
              <el-button v-if="isAuth('sys:user:update')" type="text" size="small"
                @click="addOrUpdateHandle(scope.row.userId, scope.row.deptId)">修改</el-button>
              <el-button v-if="isAuth('sys:user:delete')" type="text" size="small"
                @click="deleteHandle(scope.row.userId)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-main>

    </el-container>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 弹窗, 详情 -->
    <detail v-if="detailVisible" ref="detail" @refreshDataList="getDataList"></detail>

  </el-container>
</template>

<script>
  import AddOrUpdate from './module/user-add-or-update'
  import Detail from './module/user-detail'
  import { treeDataTranslate } from '@/utils'
  export default {
    data() {
      return {
        dataList: [],
        dataForm: {
          userName: ''
        },
        deptList: [],
        deptListTreeProps: {
          label: 'name',
          children: 'children'
        },
        deptForm: {
          id: 0,
          name: '',
          parentId: 0,
          parentName: '',
          sort: ''
        },
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        detailVisible: false
      }
    },
    components: {
      AddOrUpdate,
      Detail
    },
    activated() {
      this.deptForm.parentId = undefined
      this.deptForm.parentName = ''
      this.getDataList()
      this.getDeptList()
    },
    methods: {
      // 获取部门列表
      getDeptList() {
        this.$http({
          url: this.$http.adornUrl('/sys/dept/list'),
          method: 'get'
        }).then(({ data }) => {
          if (data.data) {
            this.deptList = treeDataTranslate(data.data, 'id')
          }
        })
      },
      // 菜单树选中
      deptListTreeCurrentChangeHandle(data, node) {
        this.deptForm.parentId = data.id
        this.deptForm.parentName = data.name
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/user/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'username': this.dataForm.userName,
            'deptId': this.deptForm.parentId
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 详情
      detailHandle(id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.get(id)
        })
      },
      // 新增 / 修改
      addOrUpdateHandle(id, deptId) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, deptId)
        })
      },
      // 删除
      deleteHandle(id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定删除指定用户?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/user/delete'),
            method: 'post',
            data: this.$http.adornData(userIds, false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => { })
      },
      resetFields() {
        this.deptForm.parentId = undefined
        this.deptForm.parentName = ''
        this.dataForm.userName = ''
      },
      tableRowClassName({ row }) {
        if (row.status === 0) {
          return 'warning-row'
        }
        return ''
      }
    }
  }
</script>

<style>
  .el-table .warning-row {
    background: rgb(255, 236, 236);
  }

  .mod-aside {
    width: 200px;
    /* background-color: rgb(226, 248, 231); */
  }

  .el-form1 {
    margin: 70px 0 0 0px;
  }

  .el-input {
    width: 190px;
  }

  .mod-input {
    width: 270px;
  }

  .el-button1 {
    height: 35px;
    width: 35px;
    padding: 9px 0px 9px 0px;
    margin: 0px 0px 0px 10px;
  }
</style>